package com.siyoumi.app.modules.app_ess.service;

import com.alibaba.fastjson.JSONObject;
import com.siyoumi.app.entity.EssQuestion;
import com.siyoumi.app.entity.EssUser;
import com.siyoumi.app.entity.SysUser;
import com.siyoumi.app.modules.app_ess.vo.VoEssQuestion;
import com.siyoumi.app.modules.app_ess.vo.VoEssQuestionAddBatch;
import com.siyoumi.app.service.EssQuestionService;
import com.siyoumi.component.XApp;
import com.siyoumi.component.XBean;
import com.siyoumi.component.XSpringContext;
import com.siyoumi.component.http.InputData;
import com.siyoumi.component.http.XHttpContext;
import com.siyoumi.exception.EnumSys;
import com.siyoumi.mybatispuls.JoinWrapperPlus;
import com.siyoumi.service.IWebService;
import com.siyoumi.util.XJson;
import com.siyoumi.util.XReturn;
import com.siyoumi.util.XStr;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//题目
@Slf4j
@Service
public class SvcEssQuestion
        implements IWebService {
    static public SvcEssQuestion getBean() {
        return XSpringContext.getBean(SvcEssQuestion.class);
    }

    static public EssQuestionService getApp() {
        return EssQuestionService.getBean();
    }


    public JoinWrapperPlus<EssQuestion> listQuery() {
        return listQuery(InputData.getIns());
    }

    /**
     * select
     *
     * @return query
     */
    public JoinWrapperPlus<EssQuestion> listQuery(InputData inputData) {
        String name = inputData.input("name");

        JoinWrapperPlus<EssQuestion> query = getApp().join();
        query.join(SysUser.table(), SysUser.tableKey(), EssUser.tableKey());
        query.eq("etqu_x_id", XHttpContext.getX());

        if (XStr.hasAnyText(name)) { //名称
            query.like("eclass_name", name);
        }

        return query;
    }

    public XReturn edit(InputData inputData, VoEssQuestion vo) {
        List<String> ignoreField = new ArrayList<>();
        if (inputData.isAdminEdit()) {
            //ignoreField.add("etqu_acc_id");
        }
        //答案
        vo.setEtqu_answer(vo.getAnswer().stream().map(item -> item.toString()).collect(Collectors.joining(",")));
        //选择项
        vo.setEtqu_question_item(XJson.toJSONString(vo.getEtqu_question_item()));
        return XApp.getTransaction().execute(status -> {
            XReturn r = getApp().saveEntity(inputData, vo, true, ignoreField);
            return r;
        });
    }

    /**
     * 批量添加题目
     */
    @Transactional(rollbackFor = Exception.class)
    public XReturn addBatch(VoEssQuestionAddBatch vo) {
        List<VoEssQuestion> importList = vo.getList();
        if (importList == null || importList.size() == 0) {
            return EnumSys.ERR_VAL.getR("导入数据异常");
        }

        List<EssQuestion> list = new ArrayList<>();
        for (VoEssQuestion item : importList) {
            EssQuestion entity = new EssQuestion();
            XBean.copyProperties(item, entity);
            entity.setEtqu_x_id(XHttpContext.getX());
            entity.setEtqu_type(vo.getType());
            entity.setEtqu_acc_id(vo.getAcc_id());
            entity.setAutoID();

            list.add(entity);
        }
        getApp().saveBatch(list);

        return EnumSys.OK.getR();
    }

    /**
     * 删除
     */
    @SneakyThrows
    @Transactional(propagation = Propagation.MANDATORY)
    public XReturn delete(List<String> ids) {
        XReturn r = XReturn.getR(0);

        getApp().delete(ids);

        return r;
    }
}
